package com.itheima52.mobilesafe.activity;

import java.util.List;

import com.itheima52.mobilesafe.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.widget.ListView;
import android.widget.TextView;

public class TaskManagerActivity extends Activity {
	@ViewInject(R.id.tv_task_process_count)
	private TextView tv_task_process_count;
	@ViewInject(R.id.tv_task_memory)
	private TextView tv_task_memory;
	@ViewInject(R.id.list_view)
	private ListView list_view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initUI();
		initData();
	}

	private void initData() {

	}

	/**
	 * ActivityManager (活动管理器,任务管理器)
	 * 
	 * PackageManager (包管理器)
	 */
	@SuppressLint("NewApi")
	private void initUI() {
		setContentView(R.layout.activity_task_manager);
		ViewUtils.inject(TaskManagerActivity.this);
		// 得到进程管理者
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		// 获得所有运行的进程
		List<RunningAppProcessInfo> runningAppProcesses = activityManager
				.getRunningAppProcesses();
		// 获取手机上面一共有多少正在运行的进程
		int size = runningAppProcesses.size();
		tv_task_process_count.setText("运行中的进程:" + size + "个");
		MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
		// 获取到内存的基本信息
		activityManager.getMemoryInfo(memoryInfo);
		// 获取到剩余的内存
		long availMem = memoryInfo.availMem;
		/**
		 * memoryInfo.totalMem不能跑到低版本,是newapi
		 */
		long totalMem = memoryInfo.totalMem;
		tv_task_memory.setText("剩余/总内存:"
				+ Formatter.formatFileSize(TaskManagerActivity.this, availMem)
				+ "/"
				+ Formatter.formatFileSize(TaskManagerActivity.this, totalMem));

	}

}
